package com.margo.project.socket.post;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

/**
 * 请求 后台查看图片，查看返回的报文
 * 
 * @author fangxingbang
 *
 */
public class ImagePostClient {
	public static void main(String[] args) {
		SocketPost();
		//httpClientPost();
	}

	/**
	 * socket请求
	 *  返回报文
	 * 
	 * HTTP/1.1 200 
	 * Content-Type: image/*
	 * Transfer-Encoding: chunked
	 * Date: Sat, 17 Nov 2018 06:25:46 GMT
	 * 
	 * 2000
	 *
	 */
	static void SocketPost() {
		InputStream inputStream=null;
		try {
			String host = "localhost";
			int port = 8091;
			Socket socket = new Socket();
			InetSocketAddress address = new InetSocketAddress(host, port);
			socket.connect(address, 10000);
			OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
			// 请求行
			osw.write("POST /api/whitelist4WebController/readAttachment HTTP/1.1\r\n");
			// 请求头
			osw.write("Content-Type: application/x-www-form-urlencoded;charset=UTF-8\r\n");
			osw.write("Content-Length: 45\r\n");
			osw.write("Host: 127.0.0.1:8091\r\n");
			osw.write("Connection: Keep-Alive\r\n");
			osw.write("User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)\r\n");
			osw.write("Accept-Encoding: gzip,deflate\r\n");
			// http协议必须在报文头后面再加一个换行，通知服务器发送完成，不然服务器会一直等待
			osw.write("\r\n");
			// 请求数据
			osw.write("attachmentId=ff80808166e2eb2d01671a52e06f006e\r\n");
			osw.write("\r\n");
			osw.flush();
			socket.shutdownOutput();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				System.out.println(line);
			}
			osw.close();
			bufferedReader.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
	}

	/**
	 * httpclient请求
	 */
	static void httpClientPost() {

		String url = "http://localhost:8091/api/whitelist4WebController/readAttachment";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("attachmentId", "ff80808166e2eb2d01671a52e06f006e"));
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			//
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				inputStream = responseEntity.getContent();
				File file = new File("C:\\Users\\fangxingbang\\Desktop\\11.jpg");
				file.createNewFile();
				outputStream = new FileOutputStream(file);
				byte[] b = new byte[100];
				int i = 0;
				while ((i = inputStream.read(b)) != -1) {
					outputStream.write(b, 0, i);
				}
			} else {
				System.out.println(":" + state + "(" + url + ")");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
